import logger from "../utils/logger";
import { dbName } from "./constant";

function isTableExist(tableName : string) {
	return new Promise<boolean>((resolve) => {
		const sql = `SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`;

		plus.sqlite.selectSql({
			name: dbName, // 你的数据库名称
			sql: sql,
			success: (res) => {
				// 如果有结果返回，说明表存在
				resolve(!!res.length);
			},
			fail: (e) => {
				logger('检查表是否存在失败:', e.message);
				resolve(false);
			}
		});
	});
}

// 使用示例
async function checkTable(tableName : string) {
	try {
		const exists = await isTableExist(tableName);
		logger('users表是否存在:', exists);
		return exists;
	} catch (e) {
		logger('检查表出错:', e);
		return false;
	}
}

export default checkTable;